<?php

namespace app\admin\controller\batchpay;

use app\admin\model\Joinpay;
use app\common\controller\Backend;
use think\Db;
use app\admin\model\batchpay\Lists;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Process extends Backend
{

    protected $msg = [
        ''
    ];

    /**
     * Lists模型对象
     * @var \app\admin\model\batchpay\Lists
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\batchpay\Lists;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    public function pass($ids)
    {
        $rows = $this->model->get(['id' => $ids]);
        $row = $rows->toArray();
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $joinpays = new Joinpay();
        $ret = $joinpays->batchPay($row);

        $msg = '';
        if ($ret['statusCode'] == '2001') {
            $status = 1;
        }else{
            $status = -2;
        }

        try {
            Db::startTrans();
            Db::name('batchpay_lists')->where('id', $ids)->update(['status' => $status]);
            Db::commit();
        }catch (\Exception $e) {
            Db::rollback();
            $this->success("提交失败");
        }

        $this->success("已提交总控");
    }

    public function refuse($ids)
    {
        $rows = $this->model->get(['id' => $ids]);
        $row = $rows->toArray();
        if (!$row) {
            $this->error(__('No Results were found'));
        }


        try {
            Db::startTrans();
            Db::name('batchpay_lists')->where('id', $ids)->update(['status' => -1]);
            Db::commit();
        }catch (\Exception $e) {
            Db::rollback();
            $this->success("提交失败");
        }

        (new Lists)->callback($rows['callbackurl'], -1,$row);

        $this->success("已拒绝");
    }

    /**
     * @return void  查询订单状态
     */
    public function check()
    {
        $param = $this->request->param();
        $ids = $param['ids'];

        $row = Db::name('batchpayLists')->where(['id' => $ids])->find();
        $row['merchantOrderNo'] = $row['ordersn'];

        $joinpays = new Joinpay();
        $ret = $joinpays->singlePayQuery($row);
        if ($ret['statusCode'] == 2001){
            $data = $ret['data'];
            $status = 202;
            if ($data['status'] == 204){

            }

        }


        $this->success("查询成功");
    }


}
